<template>
  <div class="pagination-container">
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="pageSizes"
      :small="small"
      :disabled="disabled"
      :background="background"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script setup>
import { ref, watch, defineProps, defineEmits } from 'vue'

const props = defineProps({
  total: {
    type: Number,
    required: true
  },
  page: {
    type: Number,
    default: 1
  },
  limit: {
    type: Number,
    default: 10
  },
  pageSizes: {
    type: Array,
    default: () => [10, 20, 50, 100]
  },
  layout: {
    type: String,
    default: 'total, sizes, prev, pager, next, jumper'
  },
  background: {
    type: Boolean,
    default: true
  },
  small: {
    type: Boolean,
    default: false
  },
  disabled: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['update:page', 'update:limit', 'pagination'])

// 内部变量
const currentPage = ref(props.page)
const pageSize = ref(props.limit)

// 监听props变化
watch(
  () => props.page,
  (val) => {
    currentPage.value = val
  }
)

watch(
  () => props.limit,
  (val) => {
    pageSize.value = val
  }
)

// 处理页数变化
const handleCurrentChange = (val) => {
  currentPage.value = val
  emitChange()
}

// 处理每页条数变化
const handleSizeChange = (val) => {
  pageSize.value = val
  currentPage.value = 1
  emitChange()
}

// 触发事件
const emitChange = () => {
  emit('update:page', currentPage.value)
  emit('update:limit', pageSize.value)
  emit('pagination', { page: currentPage.value, limit: pageSize.value })
}
</script>

<style scoped>
.pagination-container {
  padding: 20px 0;
  display: flex;
  justify-content: flex-end;
}
</style> 